package com.ymatou.iapi.godmch.testcase;

import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.internal.runners.TestMethod;

import com.ymatou.iapi.godmch.parameter.addPointBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.setBizConfigBean;
import com.ymatou.iapi.godmch.parameter.setPointConfigBean;
import com.ymatou.iapi.godmch.service.addPointCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.point_change_config;
import com.ymttest.database.model.godmch.point_change_configExample;
import com.ymttest.database.model.godmch.point_change_detail;
import com.ymttest.database.model.godmch.point_change_detailExample;
import com.ymttest.database.sqlwapper.godmch.point_change_configWapper;
import com.ymttest.database.sqlwapper.godmch.point_change_detailWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*添加积分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_addPoint{
	private static addPointBean bean;
	private static addPointCall call;
	Integer agent_promote_mchId=Integer.parseInt(EnvSetup.getData("agent_promote_mchId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加积分");
	}

	@Before
	public void caseUp() {
		bean=new addPointBean();
		call=new addPointCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verify(Integer agentId) throws InterruptedException
	{
		//验证
		point_change_detailWapper detailWapper=new point_change_detailWapper();
		point_change_detailExample example=new point_change_detailExample();
		example.createCriteria().andUser_idEqualTo(agentId).andPlatform_mch_idEqualTo(agent_promote_mchId);
		point_change_detail DB_datail=detailWapper.selectByExample(example).get(0);
		
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		
		point_change_configWapper configWapper=new point_change_configWapper();
		point_change_configExample configExample=new point_change_configExample(); 
		configExample.createCriteria().andChange_type_idEqualTo(100).andMch_idEqualTo(agent_promote_mchId);
		point_change_config DB_config=configWapper.selectByExample(configExample).get(0);
		
		Logger.verifyEquals(DB_config.getPoint_value(), DB_datail.getPoint_value(), "验证Point_value");
		Logger.verifyEquals(bean.getPointType(), DB_datail.getChange_type_id(), "验证Change_type_id");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addPoint_001() {
		Logger.start(true,"添加积分-101店铺分享");
		try {
//			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
//            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
//            Integer mchId=createIndepMchCall.getmchId();
			Integer mchId=agent_promote_mchId;
            //开启商户积分
            setBizConfigBean configBean=new setBizConfigBean();
            configBean.setBizId(mchId);
        	Map<String,String> map=new HashedMap();
			map.put("enable_point", "1");
			configBean.setBizConfigMap(map);
            GodmchManageCallService.setBizConfig(configBean);
            Integer agentId= GodmchManageCallService.applyAndapproveAgent(mchId).getAgentId();
            //point_change_config
            GodmchManageCallService.setPointConfig(mchId);
            
            bean.setUserId(agentId);
            bean.setPointType(101);
			call.setData(bean);
			call.callService();	
			check_verify(agentId);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addPoint_002() {
		Logger.start(true,"添加积分-102：活动分享");
		try {
			Integer mchId=agent_promote_mchId;
            //开启商户积分
            setBizConfigBean configBean=new setBizConfigBean();
            configBean.setBizId(mchId);
        	Map<String,String> map=new HashedMap();
			map.put("enable_point", "1");
			configBean.setBizConfigMap(map);
            GodmchManageCallService.setBizConfig(configBean);
            Integer agentId= GodmchManageCallService.applyAndapproveAgent(mchId).getAgentId();
            //point_change_config
            GodmchManageCallService.setPointConfig(mchId);
            
            bean.setUserId(agentId);
            bean.setPointType(102);
			call.setData(bean);
			call.callService();				
			
			check_verify(agentId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addPoint_003() {
		Logger.start(true,"添加积分-103：活动分享");
		try {
			Integer mchId=agent_promote_mchId;
            //开启商户积分
            setBizConfigBean configBean=new setBizConfigBean();
            configBean.setBizId(mchId);
        	Map<String,String> map=new HashedMap();
			map.put("enable_point", "1");
			configBean.setBizConfigMap(map);
            GodmchManageCallService.setBizConfig(configBean);
            Integer agentId= GodmchManageCallService.applyAndapproveAgent(mchId).getAgentId();
            //point_change_config
            GodmchManageCallService.setPointConfig(mchId);
            
            bean.setUserId(agentId);
            bean.setPointType(103);
			call.setData(bean);
			call.callService();				
			
			check_verify(agentId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addPoint_004() {
		Logger.start(true,"添加积分-类型不存在");
		try {
			Integer mchId=agent_promote_mchId;
            //开启商户积分
            setBizConfigBean configBean=new setBizConfigBean();
            configBean.setBizId(mchId);
        	Map<String,String> map=new HashedMap();
			map.put("enable_point", "1");
			configBean.setBizConfigMap(map);
            GodmchManageCallService.setBizConfig(configBean);
            Integer agentId= GodmchManageCallService.applyAndapproveAgent(mchId).getAgentId();
            //point_change_config
            GodmchManageCallService.setPointConfig(mchId);
            
            bean.setUserId(agentId);
            bean.setPointType(109);
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("500", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}